package com.example.administrator.zyfypt_302hrx;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.zyfypt_302hrx.bean.LoginBean;
import com.example.administrator.zyfypt_302hrx.iface.LoginListener;
import com.example.administrator.zyfypt_302hrx.model.LoginModel;


public class LoginActivity extends AppCompatActivity {
    private Button loginButton, registerButton;
    private EditText idET, pwdEt;
    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if(loginBean.getId()!=null){
                successToNext();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(),"对不起登陆失败",Toast.LENGTH_SHORT).show();
        }
    };


    private void successToNext(){
        Intent intent2 = new Intent();
        intent2.setClass(this, MainActivity.class);//forward为当前activity，forwardTarget为新的activity
        startActivity(intent2);
        finish();
    }



    private void init() {
        idET = (EditText)findViewById(R.id.et_log_username);
        pwdEt = (EditText)findViewById(R.id.et_log_pw);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
    }
    public void login(View view){
        LoginModel loginModel = new LoginModel();
        loginModel.getResult(idET.getText().toString(), pwdEt.getText().toString(), loginListener);
    }
    public void register(View view){
        Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
        startActivity(intent);
        finish();
    }

}
